<?php
namespace app\hotel\controller;

use app\hotel\model\Order as orderModel;
use app\hotel\model\Room as RoomModel;
use think\Request;
use think\Session;
use think\Db;

class Order extends Common
{
    public function order(){

        if (Request::instance()->isAjax()){

            $get = Request::instance()->get();

            $where = array();

            if (isset($get['order_sn']) && $get['order_sn'] != '' ){
                $where['order_sn'] = $get['order_sn'];
            }

            if (isset($get['order_status']) && $get['order_status'] != ''){
                $where['order_status'] = $get['order_status'];
            }

            if (isset($get['selectdate']) && $get['selectdate'] != '') {
                $datestart = strtotime(substr($get['selectdate'], 0, 10));
                $dateend = strtotime(substr($get['selectdate'], 13, 10) . '+1day');
                $where['create_time'] = array('between', array($datestart, $dateend));
            }

            $count = orderModel::orderList(array(),$where,0,0,true);

            $pagesize = $get['limit'];
            $page = (int)$get['page'];
            $startrow = ($page - 1) * $pagesize;

            $field = [];
            $data = orderModel::orderList($field,$where,$startrow,$pagesize,false);

            foreach ($data as $k => $v){

                $data[$k]['start_time'] = date('Y-m-d H:i:s',$v['start_time']);
                $data[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

            }
           
            return ['code'=>0,'msg'=>'','count'=>$count,'data'=>$data];

        }else{

            return view();

        }

    }
   /**
    * [waituserPay 代付款订单列表]
    * @Author   kendy
    * @DateTime 2019-12-18T15:05:32+0800
    * @return   [type]                   [description]
    */
    public function waituserPay()
    {  
        if (Request::instance()->isAjax()){  
            $data = orderModel::shopOrdersByPage(0);  
            $count = orderModel::shopOrdersByPage(0,true);

            return ['code'=>0,'msg'=>'','count'=>$count,'data'=>$data]; 
        }else{ 
            return view(); 
        }
    }

    /**
     * [cancelorders 取消订单列表] //orderStatus为2表示退款
     * @Author   kendy
     * @DateTime 2019-12-18T15:06:17+0800
     * @return   [type]                   [description]
     */
   public function cancelorders()
    {
      if (Request::instance()->isAjax()){  
            $data = orderModel::shopOrdersByPage(5);  
            $count = orderModel::shopOrdersByPage(5,true);  
            return ['code'=>0,'msg'=>'','count'=>$count,'data'=>$data]; 
        }else{ 
            $res = orderModel::shopOrdersByPage(5);
            $this->assign('res', $res);
            return view(); 
        }

    }
   /**
    * [outinorder 待入住订单]
    * @Author   kendy
    * @DateTime 2019-12-18T15:37:59+0800
    * @return   [type]                   [description]
    */
    public function outinorder()
    {
       if (Request::instance()->isAjax()){  
            $data = orderModel::shopOrdersByPage(1);  
            $count = orderModel::shopOrdersByPage(1,true);  
            return ['code'=>0,'msg'=>'','count'=>$count,'data'=>$data]; 
        }else{  
             $res = Db::name('hotel_order')
             ->where(["order_status"=>1,'hotel_id'=>Session::get('hotel_id')])
             ->field('order_sn')
             ->select();
             $this->assign('res', $res);
             $room_kinds = RoomModel::getOlnyRoomKindsList('room_id,room_name');
            $this->assign('room_kinds', $room_kinds); 
            return view(); 
        }  
    }
   
    /**
     * [inorder 已入住订单列表]
     * @Author   kendy
     * @DateTime 2019-12-18T15:39:14+0800
     * @return   [type]                   [description]
     */
    public function inorder()
    {

     if (Request::instance()->isAjax()){  
            $data = orderModel::shopOrdersByPages([3,4]);  
            $count = orderModel::shopOrdersByPages([3,4],true);  
            return ['code'=>0,'msg'=>'','count'=>$count,'data'=>$data]; 
        }else{ 
            $res = orderModel::shopOrdersByPages([3,4]);
             $this->assign('res', $res);
            return view(); 
        }  
    }
 

    public function cancelCode()
    {  
       if (Request::instance()->isAjax()) {  
            $res = orderModel::cancelCode(); 
            if ($res !== false) {
                return  ['code'=>1];
            } else {
                return ['code'=>2]; 
            } 

        }       
      
    }
    /**
     * [affirmOrder 待确认订单]
     * @Author   kendy
     * @DateTime 2019-12-19T13:35:15+0800
     * @return   [type]                   [description]
     */
    public function affirmorder()
    { 
        if (Request::instance()->isAjax()){  
            $data = orderModel::shopOrdersByPage(6);  
            $count = orderModel::shopOrdersByPage(6,true);  
            return ['code'=>0,'msg'=>'','count'=>$count,'data'=>$data]; 
        }else{ 
            $res = Db::name('hotel_order')->where("order_status",6)->field('order_sn')->select();
            $this->assign('res', $res);
            return view(); 
        }    
    }
  
    /**
     * [upaffirmorder 商家确认订单]
     * @Author   kendy
     * @DateTime 2019-12-19T14:00:09+0800
     * @return   [type]                   [description]
     */
    public function upaffirmorder()
    { 
      if(Request::instance()->isAjax()) { 
            $res =  orderModel::isAffirmOrder();  
            if ($res) { 
                return ['code'=>1];
            } else {
                return ['code'=>2]; 
            }  
      }   
    }
   /**
    * [getOrderDetail 获取订单详情]
    * @Author   kendy
    * @DateTime 2019-12-23T11:04:42+0800
    * @return   [type]                   [description]
    */
   public function getOrderDetail()
   {
       if (request()->isAjax()) {
            $postData = input('param.');
            $where = "order_id= {$postData['order_id']}"; 
            $hotelOrderInfo = orderModel::getOrderInfoById($where);
            return ['code'=>0,'msg'=>'','count'=>1,'data'=>$hotelOrderInfo];  
        }
   } 


   public function getRoomDetail()
   {
     if (request()->isAjax()) {
            $postData = input('param.');
            $where = "order_id= {$postData['order_id']}"; 
            $roomOrderInfo = orderModel::getRoomInfoById($where);
            return ['code'=>0,'msg'=>'','count'=>1,'data'=>$roomOrderInfo];  
        }

   } 
   

}